<?php
namespace app\client\controller;

use think\Cache;
use think\Db;
use think\Controller;
use think\Session;

class Statistics extends Controller
{

    /*
     * 销量统计
     */
    public function sales_volume()
    {

        // 销量前15的
        Db::name('shop_goods')->where(['state' => '1', 'member_id' => Session::get('shop_id')])
            ->field('title,sale_number')
            ->order(['sale_number' => 'desc'])
            ->limit(15)
            ->cache('stat_goods_list_' . Session::get('shop_id'), 300)
            ->select();

        if (empty(cache('stat_goods_list_' . Session::get('shop_id')))) $this->redirect('dashboard/index');

        return $this->fetch('', [
            'mate_title' => '销量统计'
        ]);
    }

    /*
     * 订单统计
     */
    public function order_list()
    {

        // 最近七天的订单数
        Db::name('member_order')->where(['status' => ['in', '1,2,3,4'], 'shop_id' => Session::get('shop_id')])
            ->where(['create_time' => ['egt', strtotime(date('Y-m-d 00:00:00', strtotime('-7 days')))]])
            ->field('FROM_UNIXTIME(create_time, \'%Y-%m-%d\') as time,count(*) as number')
            ->group('time')
            ->cache('stat_order_list_' . Session::get('shop_id'), 300)
            ->select();

        return $this->fetch('', [
            'mate_title' => '订单统计'
        ]);
    }


    /*
     * 清空缓存
     */
    public function wipe_cache()
    {

        if (Cache::clear()) {
            return 1;
        }
        return 0;

    }


}
